package com.lanyou.cook.repository;

import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.rest.core.annotation.RestResource;

import com.lanyou.cook.entity.kettle.KettleSlaveServer;

@RepositoryRestResource(exported=true)
public interface KettleSlaveServerRepository
		extends Repository<KettleSlaveServer, Long>{

	KettleSlaveServer findOne(Long id);
	List<KettleSlaveServer> findAll();
	
	List<KettleSlaveServer> findByName(String name);
	
	@RestResource(exported=false)
	KettleSlaveServer save(KettleSlaveServer entity);
	
	@Query(nativeQuery=true,value="SELECT t3.id,t3.nodeName,t3.ip,t3.serverPort,t3.type FROM"
			+ " ("
			+ "SELECT t1.ID_SLAVE AS id,t1.name AS nodeName,t1.HOST_NAME AS ip,t1.port AS serverPort,'0' AS type FROM `r_slave` t1"
			+ " UNION "
			+ "SELECT t2.NODE_CLUSTER_ID AS id,t2.NODE_NAME AS nodeName,t2.IP_ADDRESS AS ip,t2.SERVER_PORT AS serverPort,'1' AS type FROM `t_esb_bu_node_cluster` t2"
			+ ") t3"
			+ " WHERE ('' = :name OR t3.nodeName = :name)")
	List<Object[]> findAllServer(@Param("name") String name);
	
	@Query(nativeQuery=true,value="SELECT t3.id,t3.nodeName,t3.ip,t3.serverPort,t3.type FROM"
			+ " ("
			+ "SELECT t1.ID_SLAVE AS id,t1.name AS nodeName,t1.HOST_NAME AS ip,t1.port AS serverPort,'0' as type FROM `r_slave` t1"
			+ " UNION "
			+ "SELECT t2.NODE_CLUSTER_ID AS id,t2.NODE_NAME AS nodeName,t2.IP_ADDRESS AS ip,t2.SERVER_PORT AS serverPort ,'1' as type FROM `t_esb_bu_node_cluster` t2"
			+ ") t3"
			+ " WHERE ('' = :name OR t3.nodeName = :name)")
	List<Object[]> countAllServer(@Param("name") String name);
	
}
